﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SubSonic;
using ExtAspNet;


namespace TCMSystem.admin
{
    public partial class menu : PageBase
    {
        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        #region Module

        /// <summary>
        /// 当前页面所属的模块
        /// </summary>
        public override ModuleType Module
        {
            get
            {
                return ModuleType.CoreMenu;
            }
        }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            // 权限检查
            CheckPowerNewWithButton(btnNew);


            btnNew.OnClientClick = Window1.GetShowReference("~/admin/menu_new.aspx", "新增菜单");


            BindGrid();
        }

        private void BindGrid()
        {
            //SqlQuery q = new Select().From<TblMstMenu>();

            ////if (btnNoHiddenMenu.Pressed)
            ////{
            ////    q.Where(TblMstMenu.IsMainMenuColumn).IsEqualTo(true);
            ////}

            //q.OrderAsc(TblMstMenu.SortIndexColumn.ColumnName);
            //TblMstMenuCollection menus = q.ExecuteAsCollection<TblMstMenuCollection>();

            List<CustomMenu> menus = CustomMenuHelper.GetCustomMenus();
            Grid1.DataSource = menus;
            Grid1.DataBind();
        }


        protected string GetModuleName(object moduleNameObj)
        {
            if (moduleNameObj == null)
            {
                return String.Empty;
            }
            string moduleName = moduleNameObj.ToString();
            if (moduleName == "None")
            {
                return String.Empty;
            }
            return moduleName;
        }

        #endregion

        #region Events

        protected void Grid1_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            CheckPowerEditWithWindowField(Grid1, "editField");
            CheckPowerDeleteWithLinkButtonField(Grid1, "deleteField");
        }


        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            int menuId = Convert.ToInt32(Grid1.DataKeys[e.RowIndex][0]);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                if (!CheckPowerDelete())
                {
                    CheckPowerFailWithAlert();
                    return;
                }

                TblMstMenu.Delete(menuId);

                // 更新缓存
                CustomMenuHelper.UpdateCustomMenus();

                BindGrid();
            }
            //else if (e.CommandName == "Show")
            //{
            //    bool rowChecked = Convert.ToBoolean(Grid1.Rows[e.RowIndex].States[e.ColumnIndex]);

            //    TblMstMenu menu = TblMstMenu.FetchByID(menuId);
            //    menu.IsMainMenu = rowChecked;
            //    menu.Save(User.Identity.Name);

            //    BindGrid();
            //}
        }


        //protected void btnOnlyTwoLevelMenu_Click(object sender, EventArgs e)
        //{
        //    BindGrid();
        //}

        //protected void btnNoHiddenMenu_Click(object sender, EventArgs e)
        //{
        //    BindGrid();
        //}

        protected void Window1_Close(object sender, EventArgs e)
        {
            BindGrid();
        }
        #endregion

    }
}
